//
//  ViewController.m
//  UI 作业 - 豆瓣电影
//
//  Created by hesiyao on 15/12/22.
//  Copyright © 2015年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "ViewController.h"
#import "ActivityCell.h"
#import "ActivityModel.h"
#import "activityDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "NetworkHandler.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableview;
@property (nonatomic, retain) NSMutableArray *arr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"活动";
    
    self.tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableview];
    
    _tableview.delegate = self;
    _tableview.dataSource = self;
    
    _tableview.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
    [_tableview registerClass:[ActivityCell class] forCellReuseIdentifier:@"cell"];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav@2x"] forBarMetrics:UIBarMetricsDefault];
    
    [self DataHander];
}
// 数据源方法实现
- (void)DataHander
{
    [NetworkHandler GETWithURL:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php" completion:^(id result) {
        NSArray *arr = result[@"events"];
        self.arr = [ActivityModel modelHandlerCreate:arr];
        
        [self.tableview reloadData];
    }];
   
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _arr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 270;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    ActivityModel *m = _arr[indexPath.row];
    cell.titleLabel.text = m.title;
    cell.categoryLabel.text = [NSString stringWithFormat:@"类型 : %@",m.category_name];
    cell.addressLabel.text = m.address;
    // 用高亮的字符串给后面的关键字改变颜色
    // 首先要定义一个高亮字符串的类 初始化给他想要改变的字符串 然后确定更改的颜色和范围
    // 然后将字符串赋给想要赋给的字符串
    NSString *wishStr =[NSString stringWithFormat:@"感兴趣 : %@", m.wisher_count.stringValue];
    NSMutableAttributedString *wishStr1 = [[NSMutableAttributedString alloc] initWithString: wishStr];
    [wishStr1 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6, wishStr1.length - 6)];
    cell.wisherLabel.attributedText = wishStr1;
    
    NSString *partStr = [NSString stringWithFormat:@"参加 : %@",m.participant_count.stringValue];
    NSMutableAttributedString *partStr2 = [[NSMutableAttributedString alloc] initWithString:partStr];
    [partStr2 addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(5, partStr2.length - 5)];
    cell.partLabel.attributedText = partStr2;
    
    NSString *beginStr = m.begin_time;
    NSString *endStr = m.end_time;
    NSString *newBegin = [beginStr substringFromIndex:5];
    NSString *newEnd = [endStr substringFromIndex:5];
    NSString *str2 = [NSString stringWithFormat:@"%@ -- %@",newBegin, newEnd];
    cell.timeLabel.text = str2;
    cell.backView.image = [UIImage imageNamed:@"bg_eventlistcell@2x"];
    cell.secView.image = [UIImage imageNamed:@"bg_share_large@2x"];
    cell.timeView.image = [UIImage imageNamed:@"icon_date@2x"];
    cell.addressView.image = [UIImage imageNamed:@"icon_spot@2x"];
    cell.categoryView.image = [UIImage imageNamed:@"icon_catalog@2x"];
    
    [cell.pictureView sd_setImageWithURL:[NSURL URLWithString:m.image]];
    
    return cell;
}
// 点击进入详情页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    activityDetailViewController *detailActivityVC = [[activityDetailViewController alloc] init];
    ActivityModel *m = _arr[indexPath.row];
    detailActivityVC.model = m;
    detailActivityVC.indexpath = indexPath;
    // push 时隐藏底部栏
    detailActivityVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailActivityVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
